SUBROUTINE util_TIMTXT(TEXTIN,TIMUSD,IUNIT)

! util_TIMTXT based on TIMER by TUH //900709-hjaaj

   USE util_global_types
   USE util_global_consts
   IMPLICIT NONE
   CHARACTER(*) :: TEXTIN
   CHARACTER :: AHOUR*6, ASEC*8, AMIN*8
   REAL(REALK) :: TIMUSD
   INTEGER :: i, ISECND, IUNIT, IHOURS, MINUTE
   CHARACTER(40) :: TEXT
  
   TEXT = TEXTIN

   ISECND = INT(TIMUSD)
!   IF (ISECND .GE. 60) THEN
   IF (.FALSE.) THEN
      MINUTE = ISECND/60
      IHOURS = MINUTE/60
      MINUTE = MINUTE - 60*IHOURS
      ISECND = ISECND - 3600*IHOURS - 60*MINUTE
      IF (IHOURS .EQ. 1) THEN
         AHOUR = ' hour '
      ELSE
         AHOUR = ' hours'
      END IF
      IF (MINUTE .EQ. 1) THEN
         AMIN = ' minute '
      ELSE
         AMIN = ' minutes'
      END IF
      IF (ISECND .EQ. 1) THEN
         ASEC = ' second '
      ELSE
         ASEC = ' seconds'
      END IF
      IF (IHOURS .GT. 0) THEN
         WRITE(IUNIT,100) TEXT, IHOURS, AHOUR, MINUTE, AMIN, ISECND, ASEC
      ELSE
         WRITE(IUNIT,200) TEXT, MINUTE, AMIN, ISECND, ASEC
      END IF
   ELSE
      WRITE(IUNIT,300) TEXT,TIMUSD
   END IF
100 FORMAT(1X,A,I4,A,I3,A,I3,A)
200 FORMAT(1X,A,     I3,A,I3,A)
300 FORMAT(1X,A,F12.2,' seconds')
   RETURN
END SUBROUTINE util_TIMTXT

!-------------------------------------------------------------------------------

SUBROUTINE util_quit(msg)
   CHARACTER(*) msg
   print *, msg
   STOP ">>> FATAL ERROR"
END SUBROUTINE util_quit

!------------------------------------------------------------------------------

SUBROUTINE util_matrix_norm(label,matrix,ndim)

   USE util_global_types
   USE util_global_consts
   IMPLICIT NONE
   CHARACTER(*), INTENT(IN) :: label
   INTEGER,      INTENT(IN) :: ndim
   REAL(REALK),  INTENT(IN) :: matrix(ndim)

   INTEGER :: i
   REAL(REALK) :: norm

   norm = 0d0
   DO i = 1, ndim
      norm = norm + matrix(i)*matrix(i)
   END DO
   WRITE(*,*) 'o util_matrix_norm: ', label,' = ', SQRT(norm)

END SUBROUTINE util_matrix_norm

!------------------------------------------------------------------------------
!
!MODULE util_utils
!
!   USE util_global_types
!   USE util_global_consts
!   IMPLICIT NONE
!   PRIVATE
!   ! Public procedures
!   PUBLIC :: util_distance_between_lines,   &
!             util_loop_progress
!
!CONTAINS
!
!!-------------------------------------------------------------------------------
!
!   FUNCTION util_cross_product(a,b)
!
!      IMPLICIT NONE
!      REAL(REALK), INTENT(IN)  :: a(3), b(3)
!      REAL(REALK) :: util_cross_product(3)
!
!      util_cross_product(1) =    a(2)*b(3) - a(3)*b(2) 
!      util_cross_product(2) = -( a(1)*b(3) - a(3)*b(1) )
!      util_cross_product(3) =    a(1)*b(2) - a(2)*b(1)
!
!   END FUNCTION util_cross_product
!
!!-------------------------------------------------------------------------------
!
!   FUNCTION util_distance_between_lines(p,q,ep,eq)
!
!      IMPLICIT NONE
!      REAL(REALK), INTENT(IN)  :: p(3), q(3), ep(3), eq(3)
!      REAL(REALK) :: util_distance_between_lines
!
!      REAL(REALK) :: d1, d2, n(3)
!
!      n = util_cross_product(ep,eq) 
!
!      d1 = DOT_PRODUCT(p,n)
!      d2 = DOT_PRODUCT(q,n)
!
!      util_distance_between_lines = ABS(d1 - d2)
!
!   END FUNCTION util_distance_between_lines
!
!!-------------------------------------------------------------------------------
!
!   SUBROUTINE util_loop_progress(i,n)
!
!      IMPLICIT NONE
!      INTEGER(INTK), INTENT(IN) :: i, n
!
!      IF (n > 10) THEN
!         IF (MOD(i,n/10)==0) print *, CEILING(100d0*i/n),'%'
!      END IF
!
!   END SUBROUTINE util_loop_progress
!
!!-------------------------------------------------------------------------------
!
!END MODULE util_utils

